package com.sita.redsun.other.test;

import com.beust.jcommander.internal.Lists;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

/**
 * Created by zhebinwang on 2017/3/21.
 * 这个类测试19号协议
 * 而且能够自动化出来轨迹
 */
public class WzbNo19ForGpsId {



    ClientMainV2 clientMainV2=new ClientMainV2();
    private List<String> gpsids;


    public static void main(String[] args){

        Cli19.allThreadIsOver=false;
        //首先获得咱们的gpsid列表
        WzbNo19ForGpsId w=new WzbNo19ForGpsId();
        w.doSetGpsIds();

        w.doTest();


    }
    private void doTest(){
        //这个数据量是启动现成的个数，目前启动的线程数，不能超过读取的gpsid的个数，否则报错。
        int maxthread=2;
        Cli19.threadRunStructs=new  ThreadRunStruct[maxthread];
        for (int i =0;i<maxthread;i++){
            Cli19.threadRunStructs[i]=new  ThreadRunStruct();
            Cli19.threadRunStructs[i].setFlag(false);
            Cli19.threadRunStructs[i].setSendnumber(0);

            ClientMainThread clientMainThread=new ClientMainThread();
            clientMainThread.setGpsid(gpsids.get(i));
            //clientMainThread.setIp("115.159.146.78");
            clientMainThread.setIp("127.0.0.1");
            clientMainThread.setPort("6200");
            clientMainThread.setThreadnumber(i);


            clientMainThread.start();

        }
        //等待都执行完了以后 打印执行结果
        while (true){
            if (Cli19.allThreadIsOver){
                doPrintSendNumber();
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    //打印发送次数
    private void doPrintSendNumber(){

        int allsendnumber=0;
        for (int x=0;x<Cli19.threadRunStructs.length;x++){
            System.out.println("线程号" + x +"发送数据==       "+Cli19.threadRunStructs[x].getSendnumber());
            allsendnumber+=Cli19.threadRunStructs[x].getSendnumber();
        }
        System.out.println("总共发送了  "+ allsendnumber+"  条数据");
    }

    //读取文件
    public static String textByDealerFilePath(String filePath) throws IOException {
        File file = new File(filePath);
        FileInputStream fis = new FileInputStream(file);
        byte[] byts = new byte[fis.available()];
        fis.read(byts);
        fis.close();
        return new String(byts, "utf-8");
    }

    //填充备用gpsid列表
    private void doSetGpsIds(){
        gpsids = Lists.newArrayList();
        try {
            String jsonStr =  textByDealerFilePath("/Users/zhebinwang/dev/srces/a.json");

            JSONObject json_test = JSONObject.fromObject(jsonStr);

            // System.out.print(json_test);

            System.out.println(json_test.get("gpsids"));

            System.out.println(json_test);

            JSONArray backBodyJson = (JSONArray) json_test.get("gpsids");


            //String StrData="[{\"name\":\"zhangsan\"},{\"age\":\"18\"}]";
            //JSONArray backBodyJson = JSONArray.fromObject(StrData ); // 首先把字符串转成 JSONArray 对象
            if(backBodyJson.size()>0){
                //这里的数据量是读取文件中的 GPSID的个数，
                //而不是起线程的个数，目前我们最多读取600个id
                for(int i=0;i<backBodyJson.size() && i<600;i++) {
                    JSONObject job = backBodyJson.getJSONObject(i); // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                    System.out.println(job.get("gpsid"));

                    gpsids.add((String)job.get("gpsid"));

                }
            }





        }catch(IOException e){
            e.printStackTrace();
        }
    }


}
